package com.cy.store.controller;

import com.cy.store.entity.Address;
import com.cy.store.service.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import util.JsonResult;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("addresses")
public class AddressController extends BaseController{
    @Autowired
    private IAddressService iAddressService;

    @RequestMapping("add_new_address")
    public JsonResult<Void> addNewAddress(Address address, HttpSession session){
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        iAddressService.addNewAddress(uid,username,address);
        return new JsonResult<>(Ok);
    }

    @RequestMapping({"/", ""})
    public JsonResult<List<Address>> getByUid(HttpSession session){
        Integer uid = getUidFromSession(session);
        List<Address> list = iAddressService.getByUid(uid);
        return new JsonResult<>(Ok,list);
    }

    @RequestMapping("{aid}/set_default")
    public JsonResult<Void> setDefault(HttpSession session,@PathVariable("aid") Integer aid){
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        iAddressService.setDefault(uid,aid,username);
        return new JsonResult<>(Ok);
    }


    @RequestMapping("{aid}/delete")
    public JsonResult<Void> delete(HttpSession session,@PathVariable("aid") Integer aid){
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        iAddressService.delete(aid,uid,username);
        return new JsonResult<>(Ok);
    }




}
